﻿namespace ExamV5.PaperInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmEditPaperDate : Page
    {
        protected Button butClose;
        protected Button butEnter;
        protected GridView DataGridInfo;
        protected DropDownList DDLEndH;
        protected DropDownList DDLEndM;
        protected DropDownList DDLStartH;
        protected DropDownList DDLStartM;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private int int_0 = 0;
        protected Label labExamName;
        protected Label labExamTime;
        protected Label labInputTime;
        protected Label labInputUser;
        protected Label labPaperFen;
        protected Label labPaperStatus;
        protected Label labSubjectName;
        private PublicFun publicFun_0 = new PublicFun();
        protected RadioButtonList RBLExamShow;
        protected RadioButtonList RBLRubricShow;
        protected ScriptManager SM;
        protected TextBox txtEndDate;
        protected TextBox txtEndIP;
        protected TextBox txtJoinExamNum;
        protected TextBox txtStartDate;
        protected TextBox txtStartIP;
        protected UpdatePanel UPPart;

        protected void butEnter_Click(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(this.txtStartDate.Text) > Convert.ToDateTime(this.txtEndDate.Text))
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BInput39", "alert('考试有效期限不合法！')", true);
            }
            else
            {
                string[] strArray2 = this.txtStartIP.Text.Trim().Split(new char[] { '.' });
                string[] strArray3 = this.txtEndIP.Text.Trim().Split(new char[] { '.' });
                if (strArray2.Length != 4)
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BInput25", "alert('IP地址不合法！')", true);
                }
                else if (strArray3.Length != 4)
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BInput25", "alert('IP地址不合法！')", true);
                }
                else
                {
                    for (int i = 0; i < strArray2.Length; i++)
                    {
                        if ((((int.Parse(strArray2[i]) < 0) || (int.Parse(strArray2[i]) > 0xff)) || (int.Parse(strArray3[i]) < 0)) || (int.Parse(strArray3[i]) > 0xff))
                        {
                            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BInput25", "alert('IP地址不合法！')", true);
                            return;
                        }
                    }
                    int num = 0;
                    if (this.txtJoinExamNum.Text.Trim() == "")
                    {
                        num = 0;
                    }
                    else
                    {
                        num = Convert.ToInt16(this.txtJoinExamNum.Text.Trim());
                    }
                    string str = this.txtStartDate.Text.Trim() + " " + this.DDLStartH.SelectedItem.Value + ":" + this.DDLStartM.SelectedItem.Value + ":00";
                    string str2 = this.txtEndDate.Text.Trim() + " " + this.DDLEndH.SelectedItem.Value + ":" + this.DDLEndM.SelectedItem.Value + ":00";
                    this.publicFun_0.ExecuteSql(string.Concat(new object[] { "update PaperInfo set OutPutType='", this.RBLRubricShow.SelectedItem.Value, "',JoinExamNum=", num, ",IsShow='", this.RBLExamShow.SelectedItem.Value, "',StartIP='", this.txtStartIP.Text.Trim(), "',EndIP='", this.txtEndIP.Text.Trim(), "',PaperStartTime='", str.Trim(), "',PaperEndTime='", str2.Trim(), "' where ID=", this.int_0 }));
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BSucc", "alert('修改试卷基本信息成功！');window.close();", true);
                }
            }
        }

        protected void DataGridInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.DataGridInfo.EditIndex = -1;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) == 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
            }
            else
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            if ((e.Row.RowIndex != -1) && !((e.Row.RowState != DataControlRowState.Edit) ? (e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate)) : false))
            {
                TextBox box = (TextBox) e.Row.Cells[0].Controls[0];
                box.Width = 150;
                box.CssClass = "text";
                box.Height = 0x13;
                box.Attributes.Add("style", "height:26px");
                TextBox box2 = (TextBox) e.Row.Cells[1].FindControl("txtMinValue");
                box2.Width = 60;
                box2.CssClass = "text";
                box2.Attributes.Add("style", "height:26px");
                TextBox box3 = (TextBox) e.Row.Cells[1].FindControl("txtMaxValue");
                box3.Width = 60;
                box3.CssClass = "text";
                box3.Attributes.Add("style", "height:26px");
            }
        }

        protected void DataGridInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.DataGridInfo.EditIndex = e.NewEditIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string str = this.DataGridInfo.DataKeys[e.RowIndex]["ID"].ToString();
                string strSql = "";
                TextBox box = (TextBox) this.DataGridInfo.Rows[e.RowIndex].Cells[0].Controls[0];
                TextBox box2 = (TextBox) this.DataGridInfo.Rows[e.RowIndex].Cells[1].FindControl("txtMinValue");
                TextBox box3 = (TextBox) this.DataGridInfo.Rows[e.RowIndex].Cells[1].FindControl("txtMaxValue");
                if (((box2.Text.Trim() != "") && (box3.Text.Trim() != "")) && !(box.Text.Trim() == ""))
                {
                    strSql = string.Concat(new object[] { "update GradeReule set ReuleName='", this.publicFun_0.CheckString(box.Text), "',MaxValue=", Convert.ToDouble(box3.Text), ",MinValue=", Convert.ToDouble(box2.Text), " where ID=", str });
                    this.publicFun_0.ExecuteSql(strSql);
                    this.DataGridInfo.EditIndex = -1;
                    this.method_0();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('所有信息都不可以为空！')", true);
                }
            }
            catch
            {
            }
        }

        private void method_0()
        {
            string selectCommandText = "";
            selectCommandText = "select * from GradeReule where PaperID=" + this.int_0 + " order by ID";
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "GradeReule");
            this.DataGridInfo.DataSource = dataSet.Tables["GradeReule"].DefaultView;
            this.DataGridInfo.DataBind();
            adapter.Dispose();
            dataSet.Dispose();
            selectConnection.Dispose();
        }

        private void method_1()
        {
            try
            {
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                connection.Open();
                SqlCommand command = new SqlCommand("select * from PaperInfo where ID=" + this.int_0, connection);
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    this.labSubjectName.Text = reader["SubjectName"].ToString();
                    this.labExamName.Text = reader["ExamName"].ToString();
                    this.labPaperFen.Text = reader["PaperFen"].ToString() + "分";
                    this.txtJoinExamNum.Text = reader["JoinExamNum"].ToString();
                    this.labExamTime.Text = reader["ExamTime"].ToString() + "分钟";
                    switch (reader["PaperStatus"].ToString())
                    {
                        case "A":
                            this.labPaperStatus.Text = "正规试卷";
                            break;

                        case "B":
                            this.labPaperStatus.Text = "练习试卷";
                            break;

                        case "C":
                            this.labPaperStatus.Text = "模拟试卷";
                            break;

                        case "D":
                            this.labPaperStatus.Text = "竞赛试卷";
                            break;
                    }
                    this.labInputUser.Text = reader["InputMan"].ToString();
                    this.labInputTime.Text = reader["InputTime"].ToString();
                    this.txtStartIP.Text = reader["StartIP"].ToString();
                    this.txtEndIP.Text = reader["EndIP"].ToString();
                    this.txtStartDate.Text = reader["PaperStartTime"].ToString().Substring(0, reader["PaperStartTime"].ToString().LastIndexOf(" "));
                    this.txtEndDate.Text = reader["PaperEndTime"].ToString().Substring(0, reader["PaperEndTime"].ToString().LastIndexOf(" "));
                    this.RBLRubricShow.SelectedIndex = -1;
                    this.RBLRubricShow.Items.FindByValue(reader["OutPutType"].ToString()).Selected = true;
                    this.DDLStartH.SelectedIndex = -1;
                    this.DDLStartM.SelectedIndex = -1;
                    this.DDLEndH.SelectedIndex = -1;
                    this.DDLEndM.SelectedIndex = -1;
                    this.DDLStartH.Items.FindByValue(Convert.ToDateTime(reader["PaperStartTime"]).Hour.ToString()).Selected = true;
                    this.DDLStartM.Items.FindByValue(reader["PaperStartTime"].ToString().Substring(reader["PaperStartTime"].ToString().IndexOf(":") + 1, 2)).Selected = true;
                    this.DDLEndH.Items.FindByValue(Convert.ToDateTime(reader["PaperEndTime"]).Hour.ToString()).Selected = true;
                    this.DDLEndM.Items.FindByValue(reader["PaperEndTime"].ToString().Substring(reader["PaperEndTime"].ToString().IndexOf(":") + 1, 2)).Selected = true;
                    this.RBLExamShow.SelectedIndex = -1;
                    this.RBLExamShow.Items.FindByValue(reader["IsShow"].ToString()).Selected = true;
                }
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
            }
            catch
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            this.int_0 = Convert.ToInt32(base.Request["ID"].ToString());
            if ((this.httpCookie_0 != null) && !(this.httpCookie_0.Value == ""))
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
                    if (this.publicFun_0.ReturnPaperData() != "Yes")
                    {
                        base.Response.Write("<script>alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')</script>");
                    }
                    else if ((this.int_0 != 0) && (this.httpCookie_2.Value != "A"))
                    {
                        this.method_1();
                        this.method_0();
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

